package com.itheima.reflect;

import com.itheima.domain.Student;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;

public class Demo03 {

    public static void main(String[] args) throws NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {

        Class<Student> clazz = Student.class;
        //全部非私有的 成员变量
        Field[] fields = clazz.getFields();

        System.out.println(Arrays.toString(fields));
        //获取所有的成员变量 包括 私有
        Field[] declaredFields = clazz.getDeclaredFields();

        System.out.println(Arrays.toString(declaredFields));


        Field address = clazz.getField("address");
        System.out.println(address);
        Field name = clazz.getDeclaredField("name");
        Field age = clazz.getDeclaredField("age");
        System.out.println(name);
        System.out.println(age);
        Student student = clazz.getConstructor().newInstance();
        address.set(student,"海淀监狱");
        System.out.println(student);
        //student.getAddress();
        //Address.get(student)
        String o = (String) address.get(student);
        System.out.println(o);
        name.setAccessible(true);
        name.set(student,"吴亦凡");
        System.out.println(student);


        age.setAccessible(true);
        age.set(student,31);
        System.out.println(student);



    }
}
